/**
 * 
 */
package com.neotiq.smartdevice;

import com.neotiq.smartdevice.model.DeviceManager;
import com.neotiq.smartdevice.model.DeviceType;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * @author hbriand
 *
 */
public class DeviceAdder extends Activity {
    public static final String TAG = "SmartDeviceAdder";

    static final String[] DEVICES = new String[] {
        "Button", "Checkbox", "ToggleButton", "RadioButton", "Spinner", "ProgressBar", "SeekBar",
    };
    
    /* (non-Javadoc)
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(TAG, "Activity State: onCreate()");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.device_adder);
        
        Spinner v = (Spinner)findViewById(R.id.spnDevice);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, DEVICES);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        v.setAdapter(adapter);
        
        
        Button add = (Button)findViewById(R.id.btnOk);
        add.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				btnAddClicked();
			}
		});
    }

    protected void btnAddClicked() {
    	Spinner v = (Spinner)findViewById(R.id.spnDevice);
    	String sel = (String)v.getSelectedItem();
    	String name = ((EditText)findViewById(R.id.txtDevice)).getText().toString();
        if (sel.equals("Button") || sel.equals("ToggleButton")) {
            addDevice(sel, name);
        } else {
            Toast.makeText(getApplicationContext(), "Not supported yet", Toast.LENGTH_SHORT).show();
        }

	}

	protected void addDevice(String text, String name) {
        DeviceType dt;
        if (text.equals("Button")) {
            dt = DeviceType.BUTTON;
        } else if (text.equals("ToggleButton")) {
            dt = DeviceType.TGL_BTN;
        } else {
            return;
        }
        DeviceManager.addDevice(dt, name);
        Intent i = new Intent(this, DevicesView.class);
        startActivity(i);            
    }

}
